package com.quan.commons.log;

import cn.hutool.core.date.DateUtil;
import com.quan.system.async.LogServiceAsync;
import com.quan.system.entity.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * 默认操作日志处理
 *
 * @author huangyang
 * @since 2020-3-26
 */
@Slf4j
@Component
//@ConditionalOnMissingBean(OperationLogPrinter.class)
public class DefaultOperationLogPrinter implements OperationLogPrinter {

    @Autowired
    private LogServiceAsync logServiceAsync;

    @Override
    public void log(LogVo vo) {

        log.info(":::日志::: 用户ID:【{}】,用户名称:【{}】, IP:【{}】,标题:【{}】,执行方法:【{}】,请求URI:【{}】,执行时间:【{}】,时间:【{}】, 参数:【{}】 ",
                vo.getUserId(), vo.getUserName(), vo.getIp(), vo.getTitle(), vo.getExecMethod(),
                vo.getRequestUri(), vo.getExecTime(), DateUtil.now(), vo.getRequestParams());

        SysLog log = new SysLog();
        BeanUtils.copyProperties(vo, log);

        // 异步调用保存日志到数据库
        this.logServiceAsync.saveLog(log);
    }

}
